﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Traxz.Models;

namespace Traxz.Models
{
    public class TraxzRepository
    {
        TraxzDBEntities db = new TraxzDBEntities();

        public TraxzRepository()
        {

        }

        public List<aspnet_Users> getAllUsers()
        {
            return db.aspnet_Users.ToList();
        }

        public List<LatLon> getListLatLon(Guid sessionId)
        {
            var list = from s in db.ExerciseSessions
                       where s.ExerciseSessionId.Equals(sessionId)
                       select s.LatLons;
            List<LatLon> listLLs = new List<LatLon>();

            foreach(LatLon item in list)
            {
                listLLs.Add(item);
            }

            return listLLs;

        }

        //return the first exercise session that is found
        public ExerciseSession getSession(Guid sessionId)
        {
            var ex = from e in db.ExerciseSessions
                       where e.ExerciseSessionId.Equals(sessionId)
                       select e;
            
            return ex.ToList()[0];
        }

        public Guid getUserGuid(String userName)
        {
            var ex = from e in db.aspnet_Users
                     where e.UserName.Equals(userName)
                     select e;

            return ex.First().UserId;
        }

        public aspnet_Users getaUser(Guid userName)
        {
            var ex = from e in db.aspnet_Users
                     where e.UserId.Equals(userName)
                     select e;

            return ex.First();
        }

        public List<ExerciseSession> getListSessions()
        {
            return getAllUsers()[0].ExerciseSessions.ToList();
        }

        public List<ExerciseSession> getListOfUserExerciseSessions(Guid userID)
        {

            var c = from u in db.aspnet_Users
                    where u.UserId.Equals(userID)
                    select u;

            aspnet_Users currUser = c.First();
            return currUser.ExerciseSessions.OrderBy(e => e.DateCreated).ToList();
        }



        public void popTestData()
        {
            List<LatLon> latLonList;
            Guid userID = getAllUsers()[0].UserId;

            for (int i = 0; i < 10; i++)
            {
                latLonList = new List<LatLon>();
                //create 100 latLons for each ES
                for (int j = 0; j < 50; j++)
                {
                    latLonList.Add(new LatLon
                    {
                        LatLonId = Guid.NewGuid(),
                        Latitude = 100 + j + i,
                        Longitude = 100 + j + i,
                        LLAveSpeed = 10f
                    });
                }
                //create 100 exercise sessions
                createExerciseSessionWithLatLonList(userID, 100 + i, 100 + i, new DateTime(),
                                               100 + i, "Test" + i, "Type" + i, new TimeSpan(1,2,4), latLonList);
            }
            
        }

        public void createExerciseSessionWithLatLonList(Guid userIDIn, float AvgSpeedIn, float CaloriesBurnedIn, DateTime DateCreatedIn,
                                               float DistanceIn, String SessionNameIn, String SessionTypeIn, TimeSpan TotalTimeIn,List<LatLon> latslons)
        {

                        ExerciseSession exerciseSess = new ExerciseSession{
                            ExerciseSessionId = Guid.NewGuid(),
                            AvgSpeed = AvgSpeedIn,
                            CaloriesBurned = CaloriesBurnedIn,
                            DateCreated = DateCreatedIn,
                            Distance = DistanceIn,
                            SessionName = SessionNameIn,
                            SessionType = SessionTypeIn,
                            TotalTime = TotalTimeIn,
                            UserId = userIDIn
                        };

                        for (int i = 0; i < latslons.Count; i++)
                        {
                            latslons[i].ExerciseSessionId = exerciseSess.ExerciseSessionId;
                            db.LatLons.Add(latslons[i]);
                        }
                        //add lsit of latons to the Exercise session
                        exerciseSess.LatLons = latslons;
                       
                        db.ExerciseSessions.Add(exerciseSess);
                        db.SaveChanges();
                        


            }

        }


    }
